package com.minelx.datamicroscope.core.query.literalcondition;

import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.NoSuchElementException;

class AutoDeterminingLiteralConditionParser implements LiteralConditionParser {
    private final List<LiteralConditionParser> literalConditionParsers;

    AutoDeterminingLiteralConditionParser(List<LiteralConditionParser> literalConditionParsers) {
        this.literalConditionParsers = literalConditionParsers;
    }

    @Override
    public boolean match(JSONObject data) {
        return literalConditionParsers.stream().anyMatch(any -> any.match(data));
    }

    @Override
    public LiteralCondition createLiteralCondition(JSONObject data) {
        return literalConditionParsers.stream()
                .filter(which -> which.match(data))
                .findFirst().orElseThrow(() -> new NoSuchElementException("no parser matched for data: " + data))
                .createLiteralCondition(data);
    }
}
